import { ifError } from "assert";

export default {
  name: 'areasTem',
  props: {
    options: {
      type:Object,
      default () {
        return {
          id:"",
          name:'',
          list:[]
        }
      }
    }
  },
  data() {
    return {
      updateID:'',
      areasChecked_notcommon:[],
      areasChecked:[],
      checkAll_common: false,
      checkAll_notcommon: false,
      areas: {},
      orSaveTem: false,
      areaTemName: ''
    }
  },
  mounted() {
    this.getAreas();
  },
  watch: {
    options(val)
  	 {
         this.areasChecked_notcommon=[];
         this.areasChecked=[];
         this.updateID=val.id;
         this.areaTemName=val.name;
         if(val.name){
            this.orSaveTem=true;
         }
         if(val.list && val.list.length>0){
            for (let index = 0; index < this.areas['非常用地域'].length; index++) {
                 const element = this.areas['非常用地域'][index];
                 if(val.list.findIndex(value=>{return value==element.code})!=-1){
                    this.areasChecked_notcommon.push(element.code);
                 };
            }
            for(let key in this.areas){
                if(key!='非常用地域'){
                    for (let index = 0; index < this.areas[key].length; index++) {
                         const element = this.areas[key][index];
                         if(val.list.findIndex(value=>{return value==element.code})!=-1){
                             this.areasChecked.push(element.code);
                         };
                    }
                }
            }
         }
  	 },
    checkAll_common(val) {
      this.areasChecked = [];
      if (val) {
        for (var key in this.areas) {
          if (key != '非常用地域') {
            for (var i = 0, o; o = this.areas[key][i++];) {
              this.areasChecked.push(o.code);
            }
          }
        }
      }
    },
    checkAll_notcommon(val) {
      this.areasChecked_notcommon = [];
      if (val) {
        for (var key in this.areas) {
          if (key == '非常用地域') {
            for (var i = 0, o; o = this.areas[key][i++];) {
              this.areasChecked_notcommon.push(o.code);
            }
          }
        }
      }
    }
  },
  methods: {
    getAreas() {
      this.$http.get('/zuanzhan/areas').then((data) => {
          this.$http.interceptors(this, data);
          this.areas = data.data.data;
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    saveAreaTem() {
      if (this.areaTemName == '') {
        this.n3Toast({
          text: '模板名字不能为空',
          type: 'danger',
          placement: 'top',
          closeOnClick: true
        });
        return false;
      }
      if (this.areasChecked.length == 0 && this.areasChecked_notcommon.length == 0) {
        this.n3Toast({
          text: '未勾选地区',
          type: 'danger',
          placement: 'top',
          closeOnClick: true
        });
        return;
      }
      let data = {
        id: this.updateID ? (this.updateID) : undefined,
        template_name: this.areaTemName,
        area_list: []
      };
      for (var i = 0, o; o = this.areasChecked[i++];) {
        for (var key in this.areas) {
          if (key != '非常用地域') {
            for (var j = 0, c; c = this.areas[key][j++];) {
              if (c.code == o) {
                data.area_list.push(c.code);
                break;
              }
            }
          }
        }
      }
      for (var i = 0, o; o = this.areasChecked_notcommon[i++];) {
        for (var key in this.areas) {
          if (key == '非常用地域') {
            for (var j = 0, c; c = this.areas[key][j++];) {
              if (c.code == o) {
                data.area_list.push(c.code);
                break;
              }
              /*		areasTemSel */
            }
          }
        }
      }
      this.$http.post((this.updateID  ? '/zuanzhan/area/template-update' : '/zuanzhan/area/template-add'), data).then((data) => {
          if (!this.$http.interceptors(this, data)) return;
          this.n3Toast({
            text: '模板保存成功',
            type: 'success',
            placement: 'top',
            closeOnClick: true
          });
          // this.areasTemSel[0] = this.areaTemName;
          this.areaTemName = '';
          this.orSaveTem = false;
          this.$emit('input','')
      		this.$emit('change','')
        })
        .catch(function (error) {
          console.log(error);
        });
    }
  }
}